package com.hy.mvc.domain.dataobject.base;

import com.hy.mvc.client.constant.ErrorCode;

/**
 * <pre>
 *    author  : Booker
 *    time    : 2017/12/11 11:29
 *    desc    : 输入描述
 *    version : v1.0
 * </pre>
 */
public class ResultDO<T> extends BaseEntity implements BaseResult{

    private T module;

    private boolean	isSuccess = true;

    private String code;

    private String msg;


    public T getModule() {
        return module;
    }

    @Override
    public boolean isSuccess() {
        return isSuccess;
    }

    @Override
    public String getMsg() {
        return msg;
    }

    @Override
    public String getCode() {
        return code;
    }

    public ResultDO<T> setModule(T module) {
        this.module = module;
        return this;
    }

    @Override
    public ResultDO<T> setSuccess(boolean isSuccess) {
        this.isSuccess = isSuccess;
        return this;
    }

    @Override
    public ResultDO<T> setCode(String code) {
        this.code = code;
        return this;
    }

    @Override
    public ResultDO<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public ResultDO<T> setReslut(T t, ErrorCode code){
        code.fillResult(this);
        this.setModule(t);
        return this;
    }

    public static <T> ResultDO<T> buildSuccessResult(T t){
        ResultDO<T> res = new ResultDO<>();
        res.setSuccess(true);
        res.setModule(t);
        return res;
    }

    public static <T> ResultDO<T> buildSuccessResult(T t, String msg){
        ResultDO<T> res = new ResultDO<>();
        res.setSuccess(true);
        res.setModule(t);
        res.setMsg(msg);
        return res;
    }

    public static <T> ResultDO<T> buildFailResult(ErrorCode errorCode){
        ResultDO<T> res = new ResultDO<>();
        res.setSuccess(false);
        res.setCode(errorCode.getErrCode());
        res.setMsg(errorCode.getErrMsg());
        return res;
    }

    public static <T> ResultDO<T> buildFailResult(String msg){
        ResultDO<T> res = new ResultDO<>();
        res.setSuccess(false);
        res.setMsg(msg);
        return res;
    }

    public static <T> ResultDO<T> buildFailResult(String code,String msg){
        ResultDO<T> res = new ResultDO<>();
        res.setSuccess(false);
        res.setMsg(msg);
        res.setCode(code);
        return res;
    }

    public static <T> ResultDO<T> buildFailResult(ResultDO rs){
        ResultDO<T> res = new ResultDO<>();
        res.setSuccess(false);
        res.setCode(rs.getCode());
        res.setMsg(rs.getMsg());
        return res;
    }


}
